package com.tianhua.javs.core.engine.handler;

import com.tianhua.javs.client.bean.JavsTranslateContextBean;
import com.tianhua.javs.client.bean.TranslateResultBean;
import com.tianhua.javs.client.enums.SegmentSceneEnum;
import com.tianhua.javs.client.utils.StringHandleUtils;
import com.tianhua.javs.client.utils.StringHelperUtils;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.engine.PipelineTranslator;
import com.tianhua.javs.core.parse.SegmentSpreadParseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description:
 * date: 2022/2/24
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class BasicParamHandler extends AbstractElementHandler {
    @Autowired
    private SegmentSpreadParseService segmentSpreadParseService;

    @Resource(name = "businessPipelineTranslator")
    private PipelineTranslator businessPipelineTranslator;

    /**
     * 处理基本变量的赋值场景
     * @param javsShell
     * @param keyWard
     * @param translateContextBean
     * @return
     */
    public TranslateResultBean handleTranslate(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean, SegmentSceneEnum sceneEnum){
        //不存在函数调用
        //1.  bo.xxxxField
        //2.  str = ""
        //3.  int = 2
        //纯数字
        if(StringHelperUtils.isNum(keyWard.trim())){
            String javaSegment = segmentSpreadParseService.getBlankPrefix(javsShell) + "Integer" + TagConstants.BLANK_ONE + javsShell.trim() + TagConstants.ROW_END;
            return TranslateResultBean.getInstance(javaSegment, true);
        }
        if(keyWard.endsWith("\"")){
            String javaSegment = segmentSpreadParseService.getBlankPrefix(javsShell) + "String" + TagConstants.BLANK_ONE + javsShell.trim()  + TagConstants.ROW_END;
            return TranslateResultBean.getInstance(javaSegment, true);
        }
        if(keyWard.endsWith("L")){
            String javaSegment = segmentSpreadParseService.getBlankPrefix(javsShell) + "Long"+ TagConstants.BLANK_ONE + javsShell.trim() + TagConstants.ROW_END;
            return TranslateResultBean.getInstance(javaSegment, true);
        }
        if(keyWard.endsWith("S")){
            String javaSegment = segmentSpreadParseService.getBlankPrefix(javsShell) + "Short"+ TagConstants.BLANK_ONE + javsShell.trim() + TagConstants.ROW_END;
            return TranslateResultBean.getInstance(javaSegment, true);
        }
        if(keyWard.contains(".")){
            String eqLeftSegment = javsShell.split("=")[0].trim();
            String businessSegment = businessPipelineTranslator.translateKeyWard(translateContextBean,javsShell,keyWard);
            if(eqLeftSegment.contains(".")){
                String classVarStr = eqLeftSegment.split("\\.")[0];
                String fieldStr = eqLeftSegment.split("\\.")[1];
                String setterMethodName = StringHandleUtils.getSetterMethod(fieldStr);
                String javaSegment = segmentSpreadParseService.getBlankPrefix(javsShell) + classVarStr + TagConstants.POINT + setterMethodName + TagConstants.BRACKET_LEFT + businessSegment.trim() + TagConstants.BRACKET_RIGHT + TagConstants.ROW_END;
                return TranslateResultBean.getInstance(javaSegment, true);
            }else {
                String javaSegment =  eqLeftSegment + TagConstants.EQ + TagConstants.BLANK_ONE + businessSegment;
                return TranslateResultBean.getInstance(javaSegment, true);
            }
        }
        return TranslateResultBean.getInstance(keyWard);
    }
}
